﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

using CMS.Core.Types;
using CMS.DataLibrary;
using System.Linq.Expressions;

namespace CMS.Core
{
    public abstract class  BaseManager<T> where T : class
    {
        /// <summary>
        /// 数据仓库
        /// </summary>
        protected Repository<T> Repository;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public BaseManager() : this(ContextFactory.CurrentContext()) { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext"></param>
        public BaseManager(DbContext dbContext)
        {
            Repository = new Repository<T>(dbContext);
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回Response类, 成功时【Data】属性等于实体</returns>
        public virtual Response Add(T entity)
        {
            Response response = new Response();

            if(Repository.Add(entity) > 0)
            {
                response.Code = 1;
                response.Message = "Add Success";
                response.Data = entity;
            }
            else
            {
                response.Code = 0;
                response.Message = "Add failed";
            }

            return response;
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回Response类, 成功时【Data】属性等于实体</returns>
        public virtual Response Update(T entity)
        {
            Response response = new Response();

            if(Repository.Update(entity) > 0)
            {
                response.Code = 1;
                response.Message = "Update Success";
                response.Data = entity;
            }
            else
            {
                response.Code = 0;
                response.Message = "Update Failed";
            }

            return response;
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回Response类, 成功时【Data】属性等于实体</returns>
        public virtual Response Delete(int ID)
        {
            Response response = new Response();
            var entity = Find(ID);
          
            if(entity == null)
            {
                response.Code = 0;
                response.Message = "账号不存在";
            }
            else if(Repository.Delete(entity) > 0)
            {
                response.Code = 1;
                response.Message = "Delete Success";
                response.Data = entity;
            }
            else
            {
                response.Code = 0;
                response.Message = "Delete Failed";       
            }

            return response;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID">实体主键</param>
        /// <returns></returns>
        public T Find(int ID)
        {
            return Repository.Find(ID);
        }

        
        /// <summary>
        /// 查找数据列表--所有数据
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<T> FindList()
        {
            return Repository.FindList();
        }

        /// <summary>
        /// 查找分页
        /// </summary>
        /// <param name="paging">分页数据</param>
        /// <returns>当前页内容</returns>
        public virtual Paging<T> FindPageList(Paging<T> paging)
        {
            paging.Items = Repository.FindPageList(paging.PageSize, paging.PageIndex, out paging.TotalItems).ToList();
            return paging;
        }

        /// <summary>
        /// 总条目数
        /// </summary>
        /// <returns>总条目数</returns>
        public virtual int Count()
        {
            return Repository.Count();
        }


    }
}
